import CodeBlock from '@theme/CodeBlock';

Create a `lib/` directory and a `prisma.ts` file inside it to set up your Prisma Client instance:

{props.database === 'prisma-postgres' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { withAccelerate } from '@prisma/extension-accelerate'

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? 
  new PrismaClient().$extends(withAccelerate())

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'postgresql' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaPg } from '@prisma/adapter-pg'

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'mysql' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaMariaDb } from '@prisma/adapter-mariadb'

const adapter = new PrismaMariaDb({
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '3306'),
  connectionLimit: 5
})

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'sqlite' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3'

const adapter = new PrismaBetterSQLite3({ url: process.env.DATABASE_URL })

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'sqlserver' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaMssql } from '@prisma/adapter-mssql'

const sqlConfig = {
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  server: process.env.DB_HOST,
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000
  },
  options: {
    encrypt: true,
    trustServerCertificate: false
  }
}

const adapter = new PrismaMssql(sqlConfig)

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'planetscale' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaPlanetScale } from '@prisma/adapter-planetscale'
import { fetch as undiciFetch } from 'undici'

const adapter = new PrismaPlanetScale({ 
  url: process.env.DATABASE_URL, 
  fetch: undiciFetch 
})

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'cockroachdb' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'
import { PrismaPg } from '@prisma/adapter-pg'

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

{props.database === 'mongodb' && (
<>
<CodeBlock language="typescript" metastring="file=lib/prisma.ts">
{`import { PrismaClient } from '../generated/prisma'

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined
}

export const prisma = globalForPrisma.prisma ?? new PrismaClient()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma`}
</CodeBlock>
</>
)}

:::note

The global pattern prevents multiple instances of Prisma Client in development due to hot reloading.

:::
